module.exports = {
  data: {
    listData: null,
    page: 1,
    isLock: false,
    finish: false,
    keyword: '',
    postApiParam: {},
  },
  //loadOptions表示外部定义的对象，将覆盖原始内部对象
  loadData() {
    return new Promise((resolve, reject) => {
      if (this.data.loadOptions && this.data.loadOptions.typeKey) {
        this.data.postApiParam[this.data.loadOptions.typeKey] = this.data.loadOptions.currentType
      }
      this.data.loadMethod({ ...this.data.postApiParam
      }).then(res => {
        this.setData({
          listData: res.data
        }, function() {
          resolve(this.data.listData)
          if (res.data.length) {
            this.computeScrollViewHeight();
          }
        })
      }).catch(err => {
        console.log(err)
        reject()
      })
    })
  },
  computeScrollViewHeight() {
    let query = wx.createSelectorQuery();
    query.select('#list').boundingClientRect();
    query.exec(res => {
      let listHeight = res[0].height - 44;
      let windowHeight = wx.getSystemInfoSync();
      let isLoading = windowHeight.screenHeight + windowHeight.safeArea.top >= listHeight;
      if (isLoading) {
        this.bottomLoading().then(() => {
          this.computeScrollViewHeight()
        }).catch((e) => {
          return;
        });
      } else {
        return;
      }
    })
  },
  changeTabHandler(e) {
    if (this.selectComponent("#search")) {
      this.selectComponent("#search").resetInput();
    }
    if (!this.data.allType) {
      throw Error('请在data中定义allType数组')
    }
    if (e.detail !== 0 && !e.detail) {
      throw Error('请检查tab传入的是否有值' + e.detail)
    }
    let type = this.data.allType[e.detail];
    if (this.data.loadOptions && this.data.loadOptions.typeKey) {
      this.data.postApiParam[this.data.loadOptions.typeKey] = type;
    }
    this.data.page = 1;
    this.data.isLock = false;
    this.data.finish = false;
    this.setData({
      keyword: '',
    })
    this.data.loadMethod({
      page: this.data.page,
      ...this.data.postApiParam
    }).then(res => {
      this.setData({
        listData: res.data
      }, function() {
        if (res.data.length) {
          this.computeScrollViewHeight();
        }
      })
    }).catch(err => {
      console.log(err)
    })
  },
  cancelHandler(e) {
    this.data.page = 1;
    this.data.isLock = false;
    this.data.finish = false;
    this.setData({
      keyword: '',
      listData: []
    })
    this.data.loadMethod({
      page: this.data.page,
      ...this.data.postApiParam
    }).then(res => {
      this.setData({
        listData: res.data
      }, function() {
        if (res.data.length) {
          this.computeScrollViewHeight();
        }
      })
    }).catch(err => {
      console.log(err)
    })
  },
  searchHandler(e) {
    this.data.page = 1
    this.setData({
      keyword: e.detail,
      listData: []
    });
    this.data.loadMethod({
      [this.data.loadOptions.typeKey]: this.data.allType ? this.data.allType[0] : '',
      page: this.data.page,
      keyword: this.data.keyword
    }).then(res => {
      this.setData({
        listData: res.data
      }, function() {
        if (res.data.length) {
          this.computeScrollViewHeight();
        }
      })
    }).catch(err => {
      console.log(err)
    })
  },
  bottomLoading() {
    return new Promise((reslove, reject) => {
      if (!this.data.isLock && !this.data.finish) {
        this.data.isLock = true;
        this.data.page = this.data.page + 1;
        this.data.loadMethod({
          page: this.data.page,
          keyword: this.data.keyword,
          ...this.data.postApiParam
        }).then(res => {
          const newData = res.data;
          if (!newData.length) {
            this.data.finish = true
            reject('暂无数据');
            return;
          }
          const currentData = [...this.data.listData, ...newData];
          this.data.isLock = false;
          this.setData({
            listData: currentData
          })
          reslove();
        }).catch(err => {
          this.data.isLock = false;
          this.data.finish = false;
          this.data.page = this.data.page - 1,
            reject('网络异常,请求失败');
        })
      } else {
        if (this.data.isLock) {
          wx.showToast({
            title: '正在加载中...',
            duration: 1500,
            icon: 'none'
          })
        }
        if (this.data.finish) {
          wx.showToast({
            title: '没有更多数据了',
            duration: 1500,
            icon: 'none'
          })
        }
        reject('没有更多数据了');
      }
    });
  },
}